<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';
		
	if (!isset($_POST['is_submitted'])) {
		$_POST['is_submitted'] = '';
	}
	if (!isset($error_message)) {
		$error_message = '';
	}	
	$cat_id = $_REQUEST["category_id"];
	$sql = "SELECT type FROM category	WHERE category_id = '".$cat_id."'";
	$result = query($sql);
	$row = mysql_fetch_object($result);
	$tamplate = 'category';
	
	if($row->type ==  'collection') $tamplate = 'collcategory';
	
	if($_POST["redirect"] || $_SESSION['redirect'] ){
		if($_POST["redirect"]) $redirect = $_POST["redirect"];
		if($_SESSION['redirect']){ 
			$redirect = $_SESSION['redirect'];
			unset($_SESSION['redirect']);
		}
	}else{
       $redirect = $_SERVER['HTTP_REFERER'];	
     }	
	
	if ($_POST['is_submitted']) {
		$error_message = '';
		$redirect = $_POST["redirect"];
    
	$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
	$result_lang = query($sql);

	while ($row_lang = mysql_fetch_object($result_lang)) {
	
	
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
			
				if (!$name)  $error_message .= 'Не е въведено заглавие на '.$row_lang->Name.'<br/>';
	}
	
	/*if (!$_POST['class'])  $error_message .= 'Не е въведен Class<br/>';
		elseif(!preg_match('/^[0-9A-Za-z\-\_]+$/', $_POST['class'])) $error_message .= 'Въведеният Class е невалиден<br/>';
	*/	
	
	
    if(!$error_message) {
		
		
		$sql = "UPDATE category SET
				  parent_id = '".intval($_POST['parent_id'])."'
				 
				 WHERE
				 category_id = '$cat_id'
				";
		query($sql);

		$sql = 'SELECT lang_id,Name,code
				FROM
				language
				WHERE
				published
					';
		$result_lang = query($sql);
		
		
		while ($row_lang = mysql_fetch_object($result_lang)) {
			
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			
			$sql = "UPDATE category_description SET
				  
				   Name = '".escape($name)."', 
				   description = '".escape($description)."', 
				   meta_key = '".escape($meta_k)."',
				   meta_description = '".escape($meta_d)."'
				WHERE
					category_id = '$cat_id'
					AND
					lang_id = '$em'
				  ";
				 
		query($sql);
			
		    $sql = "UPDATE sef_url SET
						 sef_url = 		 '".escape($_POST['sef_url'][$row_lang->lang_id])."'
					  WHERE
						tamplate = '".$tamplate."'
					  AND
						query = '".$cat_id."'
					  AND
						lang_id = '".$row_lang->lang_id."'
					  ";
					 
				query($sql);
				
		
		}
		
		
		
		
		
		
		
		
		$_SESSION['message'] .= 'Редакцията е записана успешно!';
         if($_POST["redirectForm"]){
           redirect($_POST["redirectForm"]);
		}else{
			$_SESSION['redirect'] = $redirect;
		   redirect($_SERVER['HTTP_SELF'].'?category_id='.$cat_id);
		}
    }

}
  	
	
	head_admin("Създаване на нова категория");
?>


		
		<?php if ($error_message) { ?>
        <div class="message red">
            <h3>Грешка!</h3>
            <?=$error_message?>
        </div>
        <?php } ?>
        
          <?php if ($_SESSION['message']) { ?>
			<div class="message green">
				
				<?=$_SESSION['message']?>
			</div>
			<?php 
			unset($_SESSION['message']);
			} ?>
        
        <div class="topbuttons">
            <a href="<?=$redirect?>">Обратно</a>
        </div>
        
        <h3>Редактиране на категория</h3>
		<?php

			
			$sql = "SELECT 
						category_id,
						parent_id,
						published
						
					FROM
						category
					WHERE
						category_id ='$cat_id'
					
					";
			$result_cat = query($sql);
			$row_cat = mysql_fetch_object($result_cat);
			


 		?>
			            
        <form id="category_edit_form_id" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="redirect" value="<?=$redirect?>" />
            <input type="hidden" name="category_id" value="<?=$row_cat->category_id?>" />
            <input id="redirectForm" type="hidden" name="redirectForm" value="" />
        
			
            
			<!-- Tabs beginning-->
			<div id="tabs">
        		<ul id="tabs-ui"></ul>
              	
<?php 	
			$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
			$result = query($sql);
			
			$i=0;
			$tabs="";
			while ($row_lang = mysql_fetch_object($result)) {
				$em = $row_lang->lang_id;
				$tabs.=' <li><a href="'.$_SERVER['PHP_SELF'].'?category_id='.$cat_id.'#tabs-'.$em.'">'.$row_lang->Name.'</a></li>';
					
					$sql_d = "SELECT *	
					FROM
						category_description
					WHERE
						category_id = '$cat_id'
						AND lang_id = '$em'
					
					";
					$result_cat_d = query($sql_d);
					$row_cat_d = mysql_fetch_object($result_cat_d);
					$name = $row_cat_d->Name;
					$description = $row_cat_d->description;
					$meta_d = $row_cat_d->meta_description;
					$meta_k = $row_cat_d->meta_key;
					
					
					$sql_s = "SELECT sef_url	FROM sef_url
					WHERE
						tamplate = '".$tamplate."'
						AND
						query = '".$cat_id."'
						AND 
						lang_id = '".$row_lang->lang_id."'
					";
					$result_s = query($sql_s);
					$row_s = mysql_fetch_object($result_s);
					$_POST['sef_url'][$row_lang->lang_id] = $row_s->sef_url;
					
					
				if ($_POST['is_submitted']) {
				
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			}
				
								  
			?>
            <div id="tabs-<?=$em?>">
           		<dl class="clearfix">
            		<h3>Данни на  <?=$row_lang->Name?></h3>
                    
                     <dt><label class="required">Линк <?=$row_lang->Name?>:</label>
					<?=HTML_BASE?><input  class="sef_url" id="sef_url_<?=$row_lang->lang_id?>"  name="sef_url[<?=$row_lang->lang_id?>]" type="text" value="<?=$_POST['sef_url'][$row_lang->lang_id]?>" >
                    
                    <br />

Линк за фейсбук:  <a style="color:#09F; font-size:9px;" id="fb" href="https://www.facebook.com/ApivitaBulgaria/app_285763294952177?app_data=<?=base64_encode($_POST['sef_url'][$row_lang->lang_id])?>" target="_blank">https://www.facebook.com/ApivitaBulgaria/app_285763294952177?app_data=<?=base64_encode($_POST['sef_url'][$row_lang->lang_id])?></a>
                    </dt>	
                    
            		<dt><label class="required">Заглавие на <?=$row_lang->Name?></label></dt>
            		<dd><input id="title_<?=$row_lang->lang_id?>" class="inputbox w500" name="title_<?=$row_lang->lang_id?>" type="text" value="<?=$name?>" ></dd>
            
            	 <script>
					 	/*
						$("#title_<?=$row_lang->lang_id?>").keyup(function(){
							
							$("#sef_url_<?=$row_lang->lang_id?>").val(convertToSlug($(this).val()));
						});*/
						$("#sef_url_<?=$row_lang->lang_id?>").keyup(function(){
							
							$("#sef_url_<?=$row_lang->lang_id?>").val(convertToSlug($(this).val()));
							
							$('#fb').attr('href','https://www.facebook.com/ApivitaBulgaria/app_285763294952177?app_data='+$("#sef_url_<?=$row_lang->lang_id?>").val());
							$('#fb').html('https://www.facebook.com/ApivitaBulgaria/app_285763294952177?app_data='+$("#sef_url_<?=$row_lang->lang_id?>").val());
						});
					 </script>
            	</dl>
			</div>           
            
			 <?php
				$i++;
			}
			?>
            </div>
            <?php
			$parent_id = $row_cat->parent_id;
			if ($_POST['is_submitted'])
			{
				$parent_id = $_POST['parent_id'];
			}
			
	  		?>
            
            <dt>
            <label class="required">Категория родител:</label>
            
            <select disabled="disabled" class="inputbox w250" name="parent_id"  />
                <option value="0">--Няма--</option>
				   <?php $sql_c = "SELECT
                        c.category_id,
                        d.Name
                        
                    FROM
                        category AS c,
                        category_description AS d
                    WHERE
                    
                        c.category_id = d.category_id
                        
                        AND
                        
                        d.lang_id = '1'
                        
                        AND
                        c.published
						
						AND c.category_id != '".$cat_id."'
                        ORDER BY c.order_number DESC
                        
                      ";
                    
                        $result_c = query($sql_c);
                        $i = 1;	
                        while ($row = mysql_fetch_object($result_c)) {
							
							$sel = '';
							if($row->category_id == $parent_id ) $sel = 'selected="selected"';
                    ?>
                    <option  <?=$sel?> value="<?=$row->category_id?>"><?=$row->Name?></option>
                    <?php }?>
            </select>
            
        </dt>
           
							
		</form>
            
	<script>
		$(document).ready(function() {
			
				// Tabs
			$("#tabs-ui").html('<?=$tabs?>');
			$( "#tabs" ).tabs();	
			
		});
	</script>
  
  
         
<?php
	controls($redirect, "#category_edit_form_id");
	foot_admin(); 
?>